package com.ktgj.flight.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.map.GaoDeUtil;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.flight.domain.Airport;
import com.ktgj.flight.domain.vo.AirportsVo;
import com.ktgj.flight.service.IAirportService;
import com.ktgj.flight.vo.AirportVOTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 机场Controller
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/airport")
public class AirportController extends BaseController
{
    @Autowired
    private IAirportService airportService;

    /**
     * 查询机场列表
     */
    //@PreAuthorize(hasPermi = "flight:airport:list")
    @GetMapping("/list")
    public TableDataInfo list(Airport airport)
    {
        startPage();
        List<Airport> list = airportService.selectAirportList(airport);
        return getDataTable(list);
    }

    /**
     * CRM查询机场列表
     * @param airport
     * @return
     */
    @PostMapping("/airportList")
    public TableDataInfo airportList(@RequestBody Airport airport)
    {
        startPage();
        List<Airport> list = airportService.selectAirportList(airport);
        return getDataTable(list);
    }

    /**
     * 导出机场列表
     */
    @PreAuthorize(hasPermi = "flight:airport:export")
    @Log(title = "机场", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Airport airport) throws IOException
    {
        List<Airport> list = airportService.selectAirportList(airport);
        ExcelUtil<Airport> util = new ExcelUtil<Airport>(Airport.class);
        util.exportExcel(response, list, "机场数据");
    }

    /**
     * 获取机场详细信息
     */
    //@PreAuthorize(hasPermi = "flight:airport:query")
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody Map<String, Object> map)
    {
        Long id = Long.valueOf(map.get("id").toString());
        return AjaxResult.success(airportService.selectAirportById(id));
    }

    /**
     * 新增机场
     */
    //@PreAuthorize(hasPermi = "flight:airport:add")
    @Log(title = "机场", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Airport airport)
    {
        return toAjax(airportService.insertAirport(airport));
    }

    /**
     * 修改机场
     */
    //@PreAuthorize(hasPermi = "flight:airport:edit")
    @Log(title = "机场", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Airport airport)
    {
        return toAjax(airportService.updateAirport(airport));
    }

    /**
     * 删除机场信息
     * @param map
     * @return
     */
    @PostMapping("/deleteAirportById")
    public AjaxResult deleteAirportById(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        return toAjax(airportService.deleteAirportById(id));
    }

    /**
     * 删除机场
     */
    @PreAuthorize(hasPermi = "flight:airport:remove")
    @Log(title = "机场", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(airportService.deleteAirportByIds(ids));
    }

    /**
     *  查询热门机场
     * */
    @PostMapping(value = "/selectAirportByRemenAirport")
    public AjaxResult selectAirportByRemenAirport() {
        return AjaxResult.success(airportService.selectAirportByRemenAirport());
    }

    /**
     *  机场表搜索功能
     * */
    @PostMapping(value = "/searchAirport")
    public AjaxResult searchAirport(@RequestBody(required = false) Map<String, Object> map) {
        if (map == null){
            return AjaxResult.success(airportService.selectAirportByAirport());
        }
        else {
            String airportName = map.get("airportName").toString();
            return AjaxResult.success(airportService.selectAirportByAirportName(airportName));
        }


    }

    /**
     * 查询机场列表(显示)
     * */
    @PostMapping(value = "/selectAirport")
    public AjaxResult selectAirport(){
        return AjaxResult.success(airportService.selectAirport());
    }

    /**
     * 根据传入的经纬度（高德版）查询距离最近机场数据
     * */
    @PostMapping(value = "/selectAirportByCoordinatesgd")
    public AjaxResult selectAirportByCoordinatesgd(@RequestParam("coordinate") String coordinate){
        //调用方法查询城市名称
        GaoDeUtil deUtil = new GaoDeUtil();
        String str=deUtil.getCityByCoordinate(coordinate);

        //查询对应机场数据
        List<Airport> list=airportService.selectAirportByAirportCityName(str);
        List<AirportVOTwo> airportVOTwoList = new ArrayList<>();
        //遍历集合，将需要的数据去除，填充到airportVOTwoList中
        list.forEach(item->{
            AirportVOTwo airportVOTwo = new AirportVOTwo();
            airportVOTwo.setCoordinatesgd(item.getCoordinatesgd());
            airportVOTwo.setAirportId(item.getAirportId());
            airportVOTwo.setAirportName(item.getAirportName());
            airportVOTwo.setAirportCityId(item.getAirportCityId());
            airportVOTwo.setAirportCityName(item.getAirportCityName());
            airportVOTwoList.add(airportVOTwo);
        });

        //将距离赋给对应的对象
        Double distance = 0.0;
        List<AirportVOTwo> airportVOTwos=new ArrayList<>();
        for (AirportVOTwo airportVOTwo:airportVOTwoList){
            distance=deUtil.getDistanceMeter(coordinate, airportVOTwo.getCoordinatesgd());
            airportVOTwo.setDistance(distance);
            airportVOTwos.add(airportVOTwo);
        }

        /**
         * 按照当前坐标与机场的距离进行升序排列
         * */
        airportVOTwos.sort((x, y) -> Double.compare(x.getDistance(), y.getDistance()));

        return AjaxResult.success(airportVOTwos.get(0));
    }

    @PostMapping("/queryAirPortName")
    public String queryAirPortName(@RequestParam("airportId") String airportId) {
        String airPortName = airportService.queryAirPortName(airportId);
        return airPortName;
    }

    /**
     * 出发地（按首字母排序）
     * @param map
     * @return
     */
    @PostMapping("/selectAirportCity")
    public AjaxResult selectAirportCity(@RequestBody Map<String, Object> map) {
        if (map.get("airportCityName").toString() != null){
            String airportCityName = map.get("airportCityName").toString();
            AirportsVo airportsVo = airportService.selectAirportCity(airportCityName);
            if (airportsVo != null){
                return AjaxResult.success(airportsVo);
            }
            else {
                return AjaxResult.error("获取城市列表失败");
            }
        }
        else {
            return AjaxResult.error("未获取到参数");
        }
    }

    /**
     * 获取城市三字码
     * */
    @PostMapping("/selectAirportLike")
    public List<Airport> selectAirportLike(@RequestBody Map<String, Object> map){
        String airportCityName = map.get("airportCityName").toString();
        List<Airport> airportList = airportService.selectAirportLike(airportCityName);
        return airportList;
    }

    /**
     * 获取城市名称
     * */
    @PostMapping("/selectAirportName")
    public List<Airport> selectAirportName(@RequestBody Map<String, Object> map){
        String airportCityId = map.get("airportCityId").toString();
        List<Airport> airportList = airportService.selectAirportName(airportCityId);
        return airportList;
    }

    /**
     * 三字码获取机场名称
     * */
    @PostMapping("/getAirportInfoByAirportName")
    public Airport airportName(@RequestBody Map<String, Object> map) {
        String airportId = map.get("airportId").toString();
        Airport airport = airportService.airportName(airportId);
        return airport;
    }

    /**
     * 根据机场名称获取机场三字码
     * @param airportName
     * @return
     */
    @PostMapping("/airportInfoByAirportName")
    public Airport airportInfoByAirportName(String airportName) {
        return airportService.getAirportInfoByAirportName(airportName);
    }

    /**
     * 获取机场名称
     * */
    @PostMapping("/airportNameById")
    public String airportNameById(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        String name = airportService.airportNameById(id);
        return name;
    }

    /**
     * 机票城市列表
     * */
    @PostMapping("/cityList")
    public AjaxResult cityList(){
        List<Map<String, String>> airportList = airportService.cityList();
        return AjaxResult.success(airportList);
    }
}
